import 'dart:io';

import 'package:my_app/base/utils/jd_log.dart';

// router_contract_page.dart 里加好类, 鼠标放到这个main左边的箭头点一下就好了,运行好后 切换回去
main() {
  final routerContractPage =
      File('./lib/modules/router/router_contract_page.dart');
  Map<String, bool> router = {};
  final string = routerContractPage.readAsStringSync();
  RegExp regExp = RegExp(r'(.*?): (false|true)');
  regExp.allMatches(string).forEach((element) {
    jdLog('查找到数据:${element[0]}');
    final split = element[0].toString().split(':');
    final page = split[0].trim();
    final needParams = split[1].trim() == 'true';
    router[page] = needParams;
  });

  final routerContractPageExt =
      File('./lib/modules/router/router_contract_page_ext.dart');
  final routerContractPageExt2 =
      File('./lib/modules/router/router_contract_page_ext2.dart');
  if (routerContractPageExt.existsSync()) routerContractPageExt.deleteSync();
  if (routerContractPageExt2.existsSync()) routerContractPageExt2.deleteSync();
  routerContractPageExt.createSync();
  routerContractPageExt2.createSync();
  routerContractPageExt.writeAsStringSync(
      "part of 'router_contract_page.dart';\n\n",
      mode: FileMode.append);
  routerContractPageExt.writeAsStringSync('/// 路由协议\n', mode: FileMode.append);
  routerContractPageExt.writeAsStringSync(
      'class RouterContractPageImpl extends RouterContractPage {\n',
      mode: FileMode.append);

  routerContractPageExt2.writeAsStringSync(
      "import 'package:my_app/base/router/router_contract.dart';\n\n",
      mode: FileMode.append);
  routerContractPageExt2.writeAsStringSync(
      "import 'package:my_app/modules/router/router_contract_page.dart';\n\n",
      mode: FileMode.append);
  routerContractPageExt2.writeAsStringSync(
      "List<RouterContract> get routerPageList => [\n",
      mode: FileMode.append);
  router.forEach((page, needParams) {
    final lowPage = page[0].toLowerCase() + page.substring(1);
    if (needParams) {
      routerContractPageExt.writeAsStringSync(
        '  final $lowPage = RouterContract("/$lowPage", (context, params) => $page(params: params));\n',
        mode: FileMode.append,
      );
    } else {
      routerContractPageExt.writeAsStringSync(
        '  final $lowPage = RouterContract("/$lowPage", (context, params) => $page());\n',
        mode: FileMode.append,
      );
    }
    routerContractPageExt2.writeAsStringSync(
        '  RouterContractPage.instance.$lowPage,\n',
        mode: FileMode.append);
  });
  routerContractPageExt.writeAsStringSync('}', mode: FileMode.append);
  routerContractPageExt2.writeAsStringSync('];', mode: FileMode.append);
}
